package cn.youqu.happy.module.youqu.controller.admin.systemActivity;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.systemActivity.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.systemActivity.SystemActivityDO;
import cn.youqu.happy.module.youqu.convert.systemActivity.SystemActivityConvert;
import cn.youqu.happy.module.youqu.service.systemActivity.SystemActivityService;

@Api(tags = "管理后台 - 系统活动模板")
@RestController
@RequestMapping("/youqu/system-activity")
@Validated
public class SystemActivityController {

    @Resource
    private SystemActivityService systemActivityService;

    @PostMapping("/create")
    @ApiOperation("创建系统活动模板")
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:create')")
    public CommonResult<Integer> createSystemActivity(@Valid @RequestBody SystemActivityCreateReqVO createReqVO) {
        return success(systemActivityService.createSystemActivity(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新系统活动模板")
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:update')")
    public CommonResult<Boolean> updateSystemActivity(@Valid @RequestBody SystemActivityUpdateReqVO updateReqVO) {
        systemActivityService.updateSystemActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除系统活动模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:delete')")
    public CommonResult<Boolean> deleteSystemActivity(@RequestParam("id") Integer id) {
        systemActivityService.deleteSystemActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得系统活动模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:query')")
    public CommonResult<SystemActivityRespVO> getSystemActivity(@RequestParam("id") Integer id) {
        SystemActivityDO systemActivity = systemActivityService.getSystemActivity(id);
        return success(SystemActivityConvert.INSTANCE.convert(systemActivity));
    }

    @GetMapping("/list")
    @ApiOperation("获得系统活动模板列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:query')")
    public CommonResult<List<SystemActivityRespVO>> getSystemActivityList(@RequestParam("ids") Collection<Integer> ids) {
        List<SystemActivityDO> list = systemActivityService.getSystemActivityList(ids);
        return success(SystemActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得系统活动模板分页")
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:query')")
    public CommonResult<PageResult<SystemActivityRespVO>> getSystemActivityPage(@Valid SystemActivityPageReqVO pageVO) {
        PageResult<SystemActivityDO> pageResult = systemActivityService.getSystemActivityPage(pageVO);
        return success(SystemActivityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出系统活动模板 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:system-activity:export')")
    @OperateLog(type = EXPORT)
    public void exportSystemActivityExcel(@Valid SystemActivityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SystemActivityDO> list = systemActivityService.getSystemActivityList(exportReqVO);
        // 导出 Excel
        List<SystemActivityExcelVO> datas = SystemActivityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "系统活动模板.xls", "数据", SystemActivityExcelVO.class, datas);
    }

}
